<template>
  <div
    class="history-song-item"
    @click="$store.commit('updateCurrentSong', { song: item })"
    :class="{
      active: currentSong && currentSong.id === item.id,
      playing: currentSong && currentSong.id === item.id && playing,
    }"
  >
    <van-checkbox v-show="isEditing" v-model="item.isChecked" />
    <div class="left">
      <div class="playicon">
        <span></span>
        <span></span>
        <span></span>
      </div>
    </div>

    <div class="middle e_1">
      {{ item.name }}
      <span v-if="(item.song ? item.song.alias : item.alia).length">
        ({{ item.song ? item.song.alias[0] : item.alia[0] }}) </span
      >·
      <span
        v-for="ar in item.song ? item.song.artists : item.ar"
        :key="ar.id"
        >{{ ar.name }}</span
      >
    </div>

    <div class="right" v-if="isEditing" @click.stop="dele">✖</div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  props: {
    item: Object,
    isEditing: Boolean,
  },
  computed: {
    ...mapState(["currentSong", "playing", "playlist"]),
  },

  methods: {
    dele() {
      // 判断删除的是否为最后一首,如果是,则自动下降组件
      if (this.playlist.length === 1) {
        this.$store.dispatch("confirmClearPlaylist").then((is) => {
          if (is) {
            setTimeout(() => {
              // TODO有点不合理,有待优化
              this.$parent.$parent.showMyPlayList = false;
            }, 900);
          }
        });
      } else {
        this.$store.commit("deleteOneSongInPlayList", { song: this.item });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.history-song-item {
  width: 100%;
  height: 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 5px;
  transition: 0.2s;

  .left {
    width: 40px;
    height: 100%;
  }

  .middle {
    flex: 1;
    font-size: 14px;
    line-height: 40px;

    span {
      font-size: 12px;

      &::after {
        content: "/";
      }

      &:last-of-type {
        &::after {
          content: "";
        }
      }
    }
  }

  .right {
    width: 20px;
    height: 100%;
    text-align: center;
    line-height: 35px;
  }

  &.active {
    color: skyblue;

    .left {
      position: relative;

      .playicon {
        display: flex;
        justify-content: space-around;
        width: 20px;
        height: 20px;
        position: absolute;
        top: 5px;
        left: 8px;
        z-index: 1;

        span {
          width: 15%;
          height: 100%;
          background-color: skyblue;
          display: inline-block;
          animation: loading 0.8s linear infinite alternate;
          transform-origin: bottom;
          animation-play-state: paused;

          &:nth-child(1) {
            animation-delay: -0.6s;
          }

          &:nth-child(2) {
            animation-delay: 0s;
          }

          &:nth-child(3) {
            animation-delay: -0.4s;
          }
        }
      }
    }

    &.playing {
      .left {
        .playicon {
          span {
            animation-play-state: running;
          }
        }
      }
    }
  }
}
</style>
